﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AVCUMPlatform.HelperLibrary
{
    public static class MD5EncryptHelper
    {
        /// <summary>
        /// md5 对象
        /// </summary>
        private static MD5 md5 = MD5.Create();

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toCryString"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string toCryString)
        {
            var hashmd5 = MD5.Create();
            return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(toCryString))).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// 16位的MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encrypt16(string password)
        {
            string tempStr = password;
            byte[] strArr = md5.ComputeHash(Encoding.UTF8.GetBytes(tempStr));
            string result = BitConverter.ToString((strArr), 4, 8);
            result = result.Replace("-", string.Empty);
            return result;
        }

        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encrypt32(string password)
        {
            string tempStr = password;
            StringBuilder sb = new StringBuilder();
            byte[] strArr = md5.ComputeHash(Encoding.UTF8.GetBytes(tempStr));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < strArr.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                sb.Append(strArr[i].ToString("X"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 64位的MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encrypt64(string password)
        {
            string temStr = password;
            byte[] strArr = md5.ComputeHash(Encoding.UTF8.GetBytes(temStr));
            return Convert.ToBase64String(strArr);
        }

        /// <summary>
        /// 阿里云加密Md5
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string ToAliyunMD5(this string sourceString)
        {
            byte[] source = md5.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < source.Length; i++)
            {
                sb.Append(source[i].ToString("x2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成GUID
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static string CreateGUID(int type = 1)
        {
            var uuid = "";
            switch (type)
            {
                case 0:
                    // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
                    uuid = Guid.NewGuid().ToString();
                    break;
                case 1:
                    // e0a953c3ee6040eaa9fae2b667060e09
                    uuid = Guid.NewGuid().ToString("N");
                    break;
                case 2:
                    // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12
                    uuid = Guid.NewGuid().ToString("D");
                    break;
                case 3:
                    // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}
                    uuid = Guid.NewGuid().ToString("B");
                    break;
                case 4:
                    // (ade24d16-db0f-40af-8794-1e08e2040df3)
                    uuid = Guid.NewGuid().ToString("P");
                    break;
                case 5:
                    // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}}
                    uuid = Guid.NewGuid().ToString("X");
                    break;
            }
            return uuid;
        }
    }
}
